package com.epalmpay.controller.channel.activity;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.activity.RedPackageDetailOutDTO;
import com.epalmpay.dto.activity.RedPackageInDTO;
import com.epalmpay.dto.activity.RedPackageListOutDTO;
import com.epalmpay.dto.activity.RedPackageOutDTO;
import com.epalmpay.dto.activity.RedpackageAddInDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.service.activity.IActivityRedPackageService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.Tools;

@Controller
@RequestMapping(value="/channel/activity")
public class RedPackageController extends BaseController {
     
	@Resource
	private IActivityRedPackageService activityRedPackageService;
	@Resource
    private ISysLogService sysLogService;
	
	@RequestMapping(value="/redpackageIndex")
	@ResponseBody
	public RedPackageListOutDTO  redpackageIndex(RedPackageInDTO redPackageInDTO){
		RedPackageListOutDTO result = new RedPackageListOutDTO();
		try {
			result = activityRedPackageService.getToIndexData(redPackageInDTO);
			result.setErrorCode(Rescode.SUCCESS, "处理成功");
			return result;
		} catch (ApplicationException ae) {
			result.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			result.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return result;
	}

	
	/**
	 * 显示大商户分页列表
	 * 
	 */
	//@RequestMapping(value="/redpackageList")
	@ResponseBody
	public RedPackageListOutDTO  redpackageListOld(RedPackageInDTO redPackageInDTO,PageDTO pageDTO){
		RedPackageListOutDTO redPackageListOutDTO=new RedPackageListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			
            //查询时取数据			
			if(redPackageInDTO.getIsReload()){
				//获取红包列表信息
				List<RedPackageOutDTO> redPackageOutDTOList = activityRedPackageService.
						getRedPackageList(redPackageInDTO, page);
				redPackageListOutDTO.setRedPackageOutDTOList(redPackageOutDTOList);
			}
			redPackageListOutDTO.setPage(page); 
			redPackageListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return redPackageListOutDTO;
		} catch (ApplicationException ae) { 
			redPackageListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		 catch (Exception e) { 
			 redPackageListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			 logger.error(e);
		}
		return redPackageListOutDTO;
	}

	/**
	 * 显示大商户分页列表
	 *
	 */
	@RequestMapping(value="/redpackageList")
	@ResponseBody
	public RedPackageListOutDTO  redpackageList(@RequestBody RedPackageInDTO redPackageInDTO){
		RedPackageListOutDTO redPackageListOutDTO=new RedPackageListOutDTO();
		try {
			//查询时取数据
//			if(redPackageInDTO.getIsReload()){
				//获取红包列表信息
				redPackageListOutDTO = activityRedPackageService.
						getRedPackageList(redPackageInDTO);
//			}
			redPackageListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return redPackageListOutDTO;
		} catch (ApplicationException ae) {
			redPackageListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			redPackageListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return redPackageListOutDTO;
	}


	@RequestMapping(value="/getSpecialRedpackageRoutineList")
	@ResponseBody
	public RedPackageListOutDTO  GetSpecialRedpackageList(RedPackageInDTO redPackageInDTO){
		RedPackageListOutDTO redPackageListOutDTO=new RedPackageListOutDTO();
		try {


			//查询时取数据
//			if(redPackageInDTO.getIsReload()){
				//获取红包列表信息
				List<RedPackageOutDTO> redPackageOutDTOList = activityRedPackageService.
						GetSpecialRedpackageList(redPackageInDTO);
				redPackageListOutDTO.setRedPackageOutDTOList(redPackageOutDTOList);
//			}

			redPackageListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return redPackageListOutDTO;
		} catch (ApplicationException ae) {
			redPackageListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			redPackageListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return redPackageListOutDTO;
	}
	
	/**
	 * 新增红包
	 * @param redpackageAddInDTO
	 * @return
	 */
	@RequestMapping(value="/redpackageSave")
	@ResponseBody
	public RedPackageListOutDTO  RedpackageSave(RedpackageAddInDTO redpackageAddInDTO){
		RedPackageListOutDTO result =new RedPackageListOutDTO();
		try {

		    activityRedPackageService.saveRedpackage(redpackageAddInDTO,HOME_URL);
		    result.setErrorCode(Rescode.SUCCESS, "处理成功");
		    
			String content ="修改会员登入密码";
			sysLogService.addSysLog(redpackageAddInDTO.getUserId(),redpackageAddInDTO.getUserName(),null,content,redpackageAddInDTO.getGroupId());
		    
			return result;
		} catch (ApplicationException ae) {
			result.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			result.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return result;
	}
	
	@RequestMapping(value="/redpackageDetail")
	@ResponseBody
	public RedPackageDetailOutDTO  redpackageDetail(RedPackageInDTO inDTO,PageDTO pageDTO){
		RedPackageDetailOutDTO result = new RedPackageDetailOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			result = activityRedPackageService.getRedpackageDetail(inDTO,page);
			result.setPage(page); 
			result.setErrorCode(Rescode.SUCCESS, "处理成功");
			return result;
		} catch (ApplicationException ae) {
			result.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			result.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return result;
	}



	@RequestMapping(value="/redpackageEnd")
	@ResponseBody
	public RedPackageListOutDTO  RedpackageEnd(RedPackageInDTO redpackageInDTO){
		RedPackageListOutDTO result =new RedPackageListOutDTO();
		try {

			activityRedPackageService.endRedpackage(redpackageInDTO.getId());
			result.setErrorCode(Rescode.SUCCESS, "处理成功");
			return result;
		} catch (ApplicationException ae) {
			result.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			result.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return result;
	}


	/**
	 * 通过会员id查询coupon 编辑时回显
	 * @param couponInDTO
	 * @return
	 */
	@RequestMapping(value="/goEditRedpackage")
	@ResponseBody
	public RedPackageOutDTO  goEditRedpackage(RedPackageInDTO inDTO){
		RedPackageOutDTO outDTO = new RedPackageOutDTO();

		try {
			outDTO = activityRedPackageService.queryRedpackageForEdit(inDTO.getId());
			outDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return outDTO;
		} catch (ApplicationException ae) {
			outDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			outDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return outDTO;
	}

	/**
	 * 修改红包信息
	 * @param
	 * @return
	 */
	@RequestMapping(value="/updateRedpackage")
	@ResponseBody
	public RedPackageOutDTO  updateRedpackage(@RequestBody RedPackageInDTO inDTO){
		RedPackageOutDTO outDTO = new RedPackageOutDTO();

		try {
			activityRedPackageService.editRedpackage(inDTO);
			outDTO.setErrorCode(Rescode.SUCCESS, "修改成功");

			String content ="修改红包信息";
			sysLogService.addSysLog(inDTO.getUserId(),inDTO.getUserName(),null,content,inDTO.getGroupId());

			return outDTO;
		} catch (ApplicationException ae) {
			outDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			outDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return outDTO;
	}


	/*
	*  根据红包ID列表获取详情
	* */
	@RequestMapping("/getRedpackageListById")
	@ResponseBody
	public List<Map> getRedpackageListById(@RequestBody Set<Long> redpackageIdSet) {
		return activityRedPackageService.getRedpackageListById(redpackageIdSet);
	}

}
